//
//  BaseViewController.m
//  ChinaBookingPhone
//
//  Created by 杨 锐 on 14-5-22.
//  Copyright (c) 2014年 xingheng. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@property(readwrite,nonatomic) CGPoint resultPoint;

@end

@implementation BaseViewController

- (void)viewDidLoad{
    [self.view setBackgroundColor:[UIColor HexStrWithColor:@"f6f6f6"]];
    self.app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.currentPage = 1;
    [self createNavigation];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 设置tabbar角标
-(void)setTabarBadge:(NSInteger)index badgeValue:(NSString*)badgeValue{
    [[[[self.rdv_tabBarController tabBar] items] objectAtIndex:index] setBadgeValue:badgeValue];
    [[[[self.rdv_tabBarController tabBar] items] objectAtIndex:index] setBadgeTextColor:[UIColor redColor]];
    [[[[self.rdv_tabBarController tabBar] items] objectAtIndex:index] setBadgePositionAdjustment:UIOffsetMake(-3, 2)];
}

#pragma mark 自定义导航
-(void)createNavigation
{
    //设置导航
    self.TTNav = (id)self.navigationController;
    [self.TTNav setNavBarBgWithColor:[UIColor whiteColor]];
    //设置YES,表示顶部预留空白,自定义Nav后默认为NO不留空白
    self.automaticallyAdjustsScrollViewInsets = YES;
    //导航标题
    self.navigationItem.titleView=[self navigationTitle];
    if (kiOS7) {
        //修改左右按钮边距
        UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        [fixedSpace setWidth:-10.0];
        self.navigationItem.leftBarButtonItems=@[fixedSpace,[self navigationleftbutton]];
        self.navigationItem.rightBarButtonItems=@[fixedSpace,[self navigationrightbutton]];
    }else{
        //导航左按钮
        self.navigationItem.leftBarButtonItem=[self navigationleftbutton];
        //导航右按钮
        self.navigationItem.rightBarButtonItem=[self navigationrightbutton];
    }
}

#pragma mark 设置导航栏标题
-(UIView *)navigationTitle
{
    self.navtitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, kNavHeight)];
    self.navtitle.textColor=[UIColor blackColor];
    self.navtitle.backgroundColor=[UIColor clearColor];
    self.navtitle.textAlignment=NSTextAlignmentCenter;
    self.navtitle.font=[UIFont systemFontOfSize:16];
    return self.navtitle;
}

#pragma mark 左侧按钮
-(UIBarButtonItem *)navigationleftbutton
{
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setFrame:CGRectMake(0, 0, 30, 30)];
    [leftBtn setImage:[UIImage imageNamed:@"zuo"]forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(popView) forControlEvents:UIControlEventTouchUpInside];
    self.leftButton=leftBtn;
    UIBarButtonItem *leftbar=[[UIBarButtonItem alloc]initWithCustomView:self.leftButton];
    return leftbar;
}

#pragma mark 右侧按钮
-(UIBarButtonItem *)navigationrightbutton
{
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(0, 0, 30, 30)];
    self.rightButton=rightBtn;
    UIBarButtonItem *rightbar=[[UIBarButtonItem alloc]initWithCustomView:self.rightButton];
    return rightbar;
}

#pragma mark 设置左侧按钮事件
-(void)setleftButtonAction:(SEL)sel{
    
    [self.leftButton addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 设置右侧按钮事件
-(void)setRightButtonAction:(SEL)sel{
    
    [self.rightButton addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 隐藏导航
-(void)hideNaviBar:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

#pragma mark 显示导航
-(void)showNavBar:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

#pragma mark 左侧按钮默认返回上一级
-(void)popView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 关闭模态窗口
-(void)dismssView
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 自定义非系统原生导航
//不带左右按钮的导航
-(UIView *)customNavigationNoButton:(NSString *)navTitle
{
    //自定义导航
    UIView *navbar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kStateNavHeight)];
    navbar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    navbar.userInteractionEnabled = YES;
    [self.view addSubview:navbar];
    
    //标题
    UILabel *labTitle=[[UILabel alloc]initWithFrame:CGRectMake((kUIScreenWidth/2)-110, 20, 220, kNavHeight)];
    labTitle.textColor=[UIColor whiteColor];
    labTitle.backgroundColor=[UIColor clearColor];
    labTitle.textAlignment=NSTextAlignmentCenter;
    labTitle.font=[UIFont systemFontOfSize:18];
    labTitle.text=navTitle;
    [navbar addSubview:labTitle];
    
    return navbar;
}

//带左右按钮
-(UIView *)customNavigation:(NSString *)navTitle
{
    //自定义导航
    UIView *navbar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kStateNavHeight)];
    navbar.backgroundColor = [UIColor whiteColor];
    navbar.userInteractionEnabled = YES;
    [self.view addSubview:navbar];
    
    //左按钮
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setFrame:CGRectMake(5, 26, 30, 30)];
    [leftBtn setImage:[UIImage imageNamed:@"zuo"]forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(dismssView) forControlEvents:UIControlEventTouchUpInside];
    [navbar addSubview:leftBtn];
    
    //右按钮
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(kUIScreenWidth-50 , 26, 30, 30)];
    [navbar addSubview:rightBtn];
    self.rightButton = rightBtn;
    
    //标题
    UILabel *labTitle=[[UILabel alloc]initWithFrame:CGRectMake((kUIScreenWidth/2)-110, 20, 220, kNavHeight)];
    labTitle.textColor=[UIColor blackColor];
    labTitle.backgroundColor=[UIColor clearColor];
    labTitle.textAlignment=NSTextAlignmentCenter;
    labTitle.font=[UIFont systemFontOfSize:18];
    labTitle.text=navTitle;
    [navbar addSubview:labTitle];
    
    return navbar;
}

#pragma mark 根据已经加入到BaseViewController下的某个UIView子类，来计算出在它BaseViewController中的frame
/**
 *  根据已经加入到BaseViewController下的某个UIView子类，来计算出在它BaseViewController中的frame
 *  慎用，未加入到BaseViewController中会导致死循环
 *
 *  @param view 将要计算的View
 *
 *  @return frame计算结果
 */
-(CGRect)InBaseViewFrameComputeWithTargetView:(UIView *)targetView
{
    self.resultPoint = CGPointMake(0, 0);
    CGPoint point = [self InBaseViewPointComputeWithTargetView:targetView];
    return CGRectMake(point.x, point.y,
                      targetView.frame.size.width,
                      targetView.frame.size.height);
}

-(CGPoint)InBaseViewPointComputeWithTargetView:(UIView *)targetView
{
    CGRect buffFrame = targetView.frame;
    if (targetView == self.view)
    {
        //NSLog(@"%f , %f",resultPoint.x,resultPoint.y);
        if (buffFrame.size.height == 0 &&
            buffFrame.size.width  == 0 &&
            buffFrame.origin.x    == 0 &&
            buffFrame.origin.y    == 0)
        {
            self.resultPoint = CGPointMake(targetView.frame.origin.x, targetView.frame.origin.y);
        }
        return self.resultPoint;
    }
    else
    {
        self.resultPoint = CGPointMake(self.resultPoint.x+buffFrame.origin.x,
                                  self.resultPoint.y+buffFrame.origin.y);
        return [self InBaseViewPointComputeWithTargetView:targetView.superview];
    }
}

/**
 *  隐藏tabar
 *
 */
-(void)setTabBarHidden{
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

/**
 *  显示tabar
 *
 */
-(void)showTabBar{
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

/**
 *  自定义cell
 *
 *  @param title     标题
 *  @param content   内容
 *  @param toAddress 目的地址
 *
 *  @return 返回整个视图
 */
- (UIView *)titleAndContent:(NSString *)title andContent:(NSString *)content withToAddress:(NSString *)toAddress{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = content;
    contentLabel.textAlignment = 0;
//    contentLabel.numberOfLines = 0;
    [contentLabel sizeToFit];
    if ([contentLabel.text isEqualToString:@"已确认"]) {
        contentLabel.textColor = [UIColor HexStrWithColor:@"8CEA00"];
    } else if ([contentLabel.text isEqualToString:@"未确认"]){
        contentLabel.textColor = [UIColor HexStrWithColor:@"FF2D2D"];
    }
    [bottomView addSubview:contentLabel];
    
    if ([title isEqualToString:@"地址:"] || [title isEqualToString:@"路线:"]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width + 5, contentLabel.frame.origin.y + 5, 18 / 2.0, 21 / 2.0)];
        imageView.image = [UIImage imageNamed:@"jt_xl"];
        [bottomView addSubview:imageView];
        
        UILabel *toAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width + 5, contentLabel.frame.origin.y, 200, 20)];
        toAddLabel.text = toAddress;
        [bottomView addSubview:toAddLabel];
    }
    
    return bottomView;
}

@end